package com.ruoyi.framework.security.web.authentication;

import com.ruoyi.common.utils.ip.IpUtils;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;

@Getter
@Slf4j
public class CustWebAuthenticationDetails implements Serializable {

    private static final long serialVersionUID = -5852922038892970762L;


    private final String remoteAddress;

    private final String sessionId;

    public CustWebAuthenticationDetails(HttpServletRequest context) {
        this(IpUtils.getIpAddr(context), extractSessionId(context));
    }

    public CustWebAuthenticationDetails(String remoteAddress, String sessionId) {
        this.remoteAddress = remoteAddress;
        this.sessionId = sessionId;
    }

    private static String extractSessionId(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        return (session != null) ? session.getId() : null;
    }

}
